define(function () {
    'use strict';
    function ctrl($scope, $state, $stateParams, $ionicLoading, LocalCacheService, CommonService, WebService) {
        var par = $scope.param = {};

        function init() {
            WebService.getRiskPaper().then(
                function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var question = data[i];
                        var answers = new Array();
                        $.each(question.answer_content, function (key, value) {
                            answers.push({"key": key, "value": value});
                            question.answer_content = answers;
                        });
                        question.paper_answer = '';
                        question.question_idx = i + 1;
                        par.questions.push(question);
                    }
                }
            );
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            par.questions = new Array();
            init();
        });
        $scope.submitClick = function () {
            if (readyToSubmit()) {
                $ionicLoading.show();
                var paper_answers = '';
                for (var i = 0; i < par.questions.length; i++) {
                    var question = par.questions[i];
                    paper_answers += question.question_no + '&' + question.paper_answer + '|';
                }
                //isReSubmit设置为1，标示后台不做修改用户信息操作
                var params = {paper_answer: paper_answers, isReSubmit: "1", source: CommonService.getSource()};
                WebService.submitPaper(params).then(
                    function () {
                        WebService.qryUserInfo().then(
                            function (result) {
                                $ionicLoading.hide();
                                LocalCacheService.setUser(result);
                                if ($stateParams.typeid == 1) {
                                    if($stateParams["from"]){
                                        $state.go('tab.account-risk',{from:$stateParams["from"],fromParams: $stateParams["fromParams"]});
                                    }else{
                                        $state.go('tab.account-risk');
                                    }
                                }
                                else {
                                    $state.go('tab.account-easyFinancingRisk');
                                }
                            },function(){
                                $ionicLoading.hide();
                                CommonService.showAlert({message:"测评已成功；更新用户信息失败"});
                            }
                        )
                    },function(){
                        $ionicLoading.hide();
                        CommonService.showAlert({message:"测评失败"});
                    }
                );
            }
        };
        function readyToSubmit() {
            for (var i = 0; i < par.questions.length; i++) {
                var question = par.questions[i];
                if ('0' == question.question_kind && CommonService.isStrEmpty(question.paper_answer)) {
                    CommonService.showAlert({message: '第' + question.question_idx + '题未答！'});
                    return false;
                }
                if ('1' == question.question_kind) {
                    var answer_content = question.answer_content;
                    var paper_answer = '';
                    for (var j = 0; j < answer_content.length; j++) {
                        if (answer_content[j].checked) {
                            paper_answer += answer_content[j].key + '&';
                        }
                    }
                    if (CommonService.isStrEmpty(paper_answer)) {
                        CommonService.showAlert({message: '第' + question.question_idx + '题未答！'});
                        return false;
                    } else {
                        question.paper_answer = paper_answer.substring(0, paper_answer.length - 1);
                    }
                }
            }
            if (!par.risk_warn) {
                CommonService.showAlert({message: "请勾选《本人确认已理解上述风险测评内容，愿意承担由此带来的风险。》"});
                return false;
            }
            return true;
        }
    }

    ctrl.$inject = ['$scope', '$state', '$stateParams', '$ionicLoading', 'LocalCacheService', 'CommonService', 'WebService'];
    return ctrl;
});